import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;


public class Ex1_clump_bis {

	/*
	 * Main method
	 */
	public static void main(String[] args) {		

		System.out.println(new Date());

		try {
			// input parameters
			int k = 9;
			int l = 500;
			int t = 3;						
			String genome = KmerCounter.readFile("C:\\Users\\josemfer\\Downloads\\E-coli.txt").trim();		

			// internal maps		
			HashMap<String,ArrayList<Integer>> hm = 
					new HashMap<String,ArrayList<Integer>>();
			HashMap<String,ArrayList<Integer>> hmk = 
					new HashMap<String,ArrayList<Integer>>();		

			// read k-mers (with indexes array)		
			for (int i=0;i < genome.length() - k  ; i++) {
				String km = genome.substring(i,i+k);
				if (!hm.containsKey(km)    ) { // if no contains key...									
					hm.put(km,new ArrayList<Integer>(Arrays.asList(i)));				
				} else if (hm.get(km).size() < t ) {	// if contains key
					// clump index list: 1... t-1 elements
					ArrayList<Integer> klist = hm.get(km);			
					do {
						// Is there space in clump for a new element?
						if ( klist.size() == 0 || 
								i - klist.get(0) < l - (t - klist.size())*k) {
							// there is space
							klist.add(i);
							if (klist.size() == t) {
								hmk.put(km, klist);
							}
							break;
						} else {  // no space
							klist.remove(0);
						}							
					} while (true); // END do loop						
				} // END IF contains key							
			} // END for loop genome

			System.out.println(hmk.size());
			System.out.println(new Date());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	} // main	
}
